/**
 * 
 */
package il.ac.bgu.cs.heuristics.graph;

import il.ac.bgu.cs.heuristics.graph.types.IAction;
import il.ac.bgu.cs.heuristics.graph.types.IState;

import org.jgrapht.graph.DefaultWeightedEdge;

/**
 * 
 * NOTE: extends a class which is marked for serialization however, this class
 * is not designed for serialization
 * 
 * @author Ronen Pinko
 * 
 */
final public class ActionEdge extends DefaultWeightedEdge { // NOPMD cyclomatic
	private static final long		serialVersionUID	= 249473310269059634L;
	private transient final IState	from;
	private transient final IState	to;
	private transient final IAction	action;
	private final boolean			isNonContradictingEdge;

	public ActionEdge(IState from, IState to, boolean isNonContradicting) {
		super();
		this.from = from;
		this.to = to;
		isNonContradictingEdge = isNonContradicting;
		this.action = new GraphAction(from, to);
	}

	public IState getFrom() {
		return from;
	}

	public IState getTo() {
		return to;
	}

	public IAction getAction() {
		return action;
	}

	public boolean isNonContradicting() {
		return isNonContradictingEdge;
	}

	@Override
	protected double getWeight() {
		return isNonContradictingEdge ? 0 : 1;
	}

	@Override
	protected Object getSource() {
		return from;
	}

	@Override
	protected Object getTarget() {
		return to;
	}

	@Override
	public String toString() {
		return "ActionEdge [action=" + action + ", from=" + from
				+ ", isNonContradictingEdge=" + isNonContradictingEdge
				+ ", to=" + to + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) { // NOPMD cyclomatic complexity
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final ActionEdge other = (ActionEdge) obj;
		if (from == null) {
			if (other.from != null) {
				return false;
			}
		} else if (!from.equals(other.from)) {
			return false;
		}
		if (to == null) {
			if (other.to != null) {
				return false;
			}
		} else if (!to.equals(other.to)) {
			return false;
		}
		return true;
	}

}